package com.zc.im.service.im.group.impl;

import com.zc.im.mapper.im.group.ZcImGroupCommentMapper;
import com.zc.im.model.im.group.ZcImGroupComment;
import com.zc.im.model.im.group.ZcImGroupCommentExample;
import com.zc.im.model.im.group.ZcImGroupCommentKey;
import com.zc.im.service.im.group.ImGroupCommentService;
import com.zc.im.util.ImGolbalTypeContext;
import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.ModCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 评论表
 * Created by wangcc on 2017/7/8.
 */
@Service
public class ImGroupCommentServiceImpl implements ImGroupCommentService {

    @Autowired
    private ZcImGroupCommentMapper groupCommentMapper;
    private static Logger logger = Logger.getLogger(ImGroupCommentServiceImpl.class);


    /**
     * 添加评论
     * @param zcImGroupCommentModel
     * @return
     */
    @Override
    public Integer insertGroupComment(ZcImGroupComment zcImGroupCommentModel) {
        Integer groupCommentId = null;
        if (zcImGroupCommentModel == null) {
            logger.info("insert GroupComment is null");
            return groupCommentId;
        }
        zcImGroupCommentModel.setCreatedTime(new Date());
        zcImGroupCommentModel.setStatus(ImGolbalTypeContext.STATUS_VALID);
        try {
            groupCommentId = groupCommentMapper.insertSelective(zcImGroupCommentModel);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupCommentId;
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @Override
    public Integer deleteGroupComment(Integer id) {
        Integer groupCommentId = null;
        if (id == null) {
            logger.info("delete GroupComment is null");
            return groupCommentId;
        }
        try {
            ZcImGroupCommentKey key = new ZcImGroupCommentKey();
            key.setId(id);
            groupCommentId = groupCommentMapper.deleteByPrimaryKey(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupCommentId;
    }

    /**
     * 更新评论
     * @param groupComment
     * @return
     */
    @Override
    public Integer updateGroupComment(ZcImGroupComment groupComment) {
        Integer groupCommentId = null;
        if (groupComment == null) {
            logger.info("update GroupComment is null");
            return groupCommentId;
        }
        try {
            groupComment.setUpdatedTime(new Date());
            ZcImGroupCommentExample groupCommentExample = new ZcImGroupCommentExample();
            ZcImGroupCommentExample.Criteria criteria =  groupCommentExample.createCriteria();
            criteria.andIdEqualTo(groupComment.getId());
            groupCommentId = groupCommentMapper.updateByExampleSelective(groupComment, groupCommentExample);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupCommentId;

    }

    /**
     * //通过日志logid查询评论
     * @param zcImGroupCommentModel
     * @return
     */
    @Override
    public List<ZcImGroupComment> queryGroupComment(ZcImGroupComment zcImGroupCommentModel) {
        List<ZcImGroupComment> groupCommentModels = new ArrayList<>();
        if (zcImGroupCommentModel == null) {
            logger.info("get groupcomment is null");
            return groupCommentModels;
        }
        try {
            ZcImGroupCommentExample groupCommentExample = new ZcImGroupCommentExample();
            ZcImGroupCommentExample.Criteria criteria =  groupCommentExample.createCriteria();
            criteria.andLogIdEqualTo(zcImGroupCommentModel.getLogId());
            groupCommentModels = groupCommentMapper.selectByExample(groupCommentExample);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupCommentModels;
    }


    /**
     * 添加评论
     * @param groupComment
     * @return
     */
    @Override
    public Integer saveGroupComment(ZcImGroupComment groupComment) {
        Integer groupCommentKey=null;
        if (groupComment==null) {
            return  groupCommentKey;
        }
        groupComment.setCreatedTime(new Date());
        try {
            List<ZcImGroupComment> listzcImGroupComment=queryGroupComment(groupComment);
            if (listzcImGroupComment == null || listzcImGroupComment.size()==0) {
                groupCommentKey= insertGroupComment(groupComment);
            } else {
                logger.error("不能重复评论");
                return groupCommentKey;
            }

        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return groupCommentKey;
    }


    /**
     * 根据日志id查评论
     * @param logId
     * @return
     */
    @Override
    public List<ZcImGroupComment> listGroupComment(Integer logId) {
        List<ZcImGroupComment> listZcImGroupComment = null;
        if (logId==null) {
            return  listZcImGroupComment;
        }
        ZcImGroupComment zcImGroupComment = new ZcImGroupComment();
        zcImGroupComment.setLogId(logId);
        try {
            listZcImGroupComment = queryGroupComment(zcImGroupComment);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return listZcImGroupComment;
    }
}

